///////////////////////////////////////////////////////////
//  SendMailCommand.cpp
//  Implementation of the Class SendMailCommand
//  Created on:      21-Sep-2013 12:10:26 PM
//  Original author: Jakkaphan Aiemcharoenjit
///////////////////////////////////////////////////////////

#include "SendMailCommand.h"
#include "CurrentCommand.h"
#include "s3eEMail.h"
#include "IInt.h"
#include "IHeader.h"
#include "IItem.h"

using namespace UI;
using namespace UI::CMD;

SendMailCommand::SendMailCommand(CSendMailView* sendMail, CCheckListView* checkList)
{
  this->m_type = ICommand::SENDMAIL_CMD;
  this->m_checkListView = checkList;
  this->m_sendMailView = sendMail;
}


SendMailCommand::~SendMailCommand()
{

}


void SendMailCommand::Execute()
{
  s3eEMail *mail = new s3eEMail();
  memset(mail, 0, sizeof(s3eEMail));

  IPage* curPage = this->m_checkListView->GetCurSelected();
  mail->m_subject = curPage->GetName().c_str();
  mail->m_messageBody = bodyContext(curPage).c_str();
  mail->m_numToRecipients = 1;

  if(s3eEMailSendMail(mail) == S3E_RESULT_SUCCESS)
    CurrentCommand::GetInstance()->SetCurrentCommand(this->m_type);

  // Set current command
  //CurrentCommand::GetInstance()->SetCurrentCommand(this->m_type);
}


ICommand::CommandType SendMailCommand::GetType()
{
	return this->m_type;
}

std::string UI::CMD::SendMailCommand::bodyContext(IPage* page)
{
  std::string result;
  StringStream_t bodyTextMail;
  /*result += "CheckList name: " + page->GetName() + "\n";
  result += "Template name: " + page->GetTemplateName() + "\n";
  int childItems = page->CountChilds();
  IChoices* choices = page->GetChoices();
  for(int index = 0; index < childItems; index++)
  {
    IObject* obj = page->GetChild(index);
    switch(obj->GetType())
    {
    case IObject::ITEM_T:
      {
        IItem* item = dynamic_cast<IItem*>(obj);
        result += item->GetName() + "          ";
        int choiceIndex = dynamic_cast<IInt*>(item->GetData())->GetValue();
        if(!choices->GetChoice(choiceIndex).empty())
          result += choices->GetChoice(choiceIndex) + "\n";
        else
          result +=  + "-\n";
      }
      break;
    case IObject::HEARDER_T:
      {
         IHeader* header = dynamic_cast<IHeader*>(obj);
         result += header->GetName() + "\n";
         int childCount = header->CountChilds();
         for(int childIndex = 0; childIndex < childCount; childIndex++)
         {
           IItem* childItem = dynamic_cast<IItem*>(header->GetChild(childIndex));

           result += childItem->GetName() + "          ";
           int choiceIndex = dynamic_cast<IInt*>(childItem->GetData())->GetValue();
           if(!choices->GetChoice(choiceIndex).empty())
             result += choices->GetChoice(choiceIndex) + "\n";
           else
             result +=  + "-\n";
         }
      }
    default: break;
    }
  }*/

  bodyTextMail << _T("CheckList name: ") << page->GetName() << std::endl;
  bodyTextMail << _T("Template name: ") << page->GetTemplateName() << std::endl;
  int childItems = page->CountChilds();
  IChoices* choices = page->GetChoices();
  for(int index = 0; index < childItems; index++)
  {
    IObject* obj = page->GetChild(index);
    switch(obj->GetType())
    {
    case IObject::ITEM_T:
      {
        IItem* item = dynamic_cast<IItem*>(obj);
        bodyTextMail.width(100);
        bodyTextMail << std::left << item->GetName();
        int choiceIndex = dynamic_cast<IInt*>(item->GetData())->GetValue();
        bodyTextMail.width(24);
        if(!choices->GetChoice(choiceIndex).empty())
          bodyTextMail << std::right << choices->GetChoice(choiceIndex) << std::endl;
        else
          bodyTextMail << std::right << _T("-") << std::endl;
      }
      break;
    case IObject::HEARDER_T:
      {
        IHeader* header = dynamic_cast<IHeader*>(obj);
        bodyTextMail << header->GetName() << std::endl;
        int childCount = header->CountChilds();
        for(int childIndex = 0; childIndex < childCount; childIndex++)
        {
          IItem* childItem = dynamic_cast<IItem*>(header->GetChild(childIndex));
          bodyTextMail.width(7);
          bodyTextMail << std::right << _T("- ");
          bodyTextMail.width(100);
          bodyTextMail << std::left << childItem->GetName();
          bodyTextMail.width(24);
          int choiceIndex = dynamic_cast<IInt*>(childItem->GetData())->GetValue();
          if(!choices->GetChoice(choiceIndex).empty())
            bodyTextMail << std::right << choices->GetChoice(choiceIndex) << std::endl;
          else
            bodyTextMail << std::right << _T("-") << std::endl;
        }
      }
    default: break;
    }
  }

  return bodyTextMail.str();
}
